package com.turing.post.cardmanagement.util;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import com.turing.post.util.PCPSUtil;

/**
 * 已激活卡列表
 * @author kz
 * @date 2012-12-17
 */
@SuppressWarnings("serial")
@Entity
@Table(name = "ActivateCard")
public class ActivateCard implements Serializable {

	public ActivateCard() {
		super();
	}
    /**
     *制卡批次号
     */
	@Column(name = "BATCHNO", nullable = false)
	private String batchNo;
	
	/**
	 *卡顺序号
	 */
	@Column(name = "CARDSEQNO", nullable = false)
	private String cardSeqNo;
	
	/**
	 *卡号
	 */
	@Id
	@Column(name = "CARDNO", nullable = false)	
	private String cardNo;

	/**
	 *卡面ID
	 */
	@Column(name = "CARDFACEID", nullable = false)
	private BigInteger cardFaceId;

	/**
	 *卡片种类ID
	 */
	@Column(name = "CARDCLASSICID", nullable = false)
	private BigInteger cardClassicId;
	
	/**
	 * 主帐号
	 */
	@Column(name = "MASTERACCNO", nullable = true)
	private BigInteger masterAccNo;
	
	/**
	 * 钱包主帐号
	 */
	@Column(name = "WALLETACCNO", nullable = true)
	private BigInteger walletAccNo;
	
	/**
	 * 卡密文密码
	 */
	@Column(name = "CARDPWD", nullable = false)
	private String cardPwd;
	
	/**
	 * 二磁道内容
	 */
	@Column(name = "TWOTRACKCONTENT", nullable = false)
	private String twoTrackContent;
	
	/**
	 * 三磁道内容
	 */
	@Column(name = "THIRDTRACKCONTENT", nullable = false)
	private String thirdTrackContent;
	
	/**
	 * 销售机构
	 */
	@Column(name = "SALEBRANCHNO", nullable = false)
	private String saleBranchNo;

	/**
	 * 开户机构
	 */
	@Column(name = "OPENBRANCHNO", nullable = false)
	private String openBranchNo;

	/**
	 * 销售单ID
	 */
	@Column(name = "SALEORDERID", nullable = true)
	private BigInteger saleOrderId;
	
	/**
	 * 购卡人ID
	 */
	@Column(name = "BUYERID", nullable = true)
	private BigInteger buyerId;
	
	/**
	 * 持有人ID
	 */
	@Column(name = "OWNERID",nullable = true)
	private BigInteger ownerId;
	
	/**
	 * 初始余额
	 */
	@Column(name = "INITIALBALANCE", nullable = false)
	private BigInteger initialBalance;
	
	/**
	 *  累计充值
	 */
	@Column(name = "TOTALDEPOSIT", nullable = false)
	private BigInteger totalDeposit;
		
	/**
	 * 当前余额
	 */
	@Column(name = "BALANCE", nullable = false)
	private BigInteger balance;

	/**
	 * 当前钱包余额
	 */
	@Column(name = "WALLETBALANCE", nullable = false)
	private BigInteger walletBalance;

	/**
	 * 有效期
	 */
	@Column(name = "EXPIRED", nullable = true)
	private Date expired;
	
	/**
	 * 上次扣收过期管理费日期
	 */
	@Column(name = "LASTEXPIREDFEEDATE", nullable = true)
	private Date lastExpiredFeeDate; 
	
	/**
	 * 密码错误次数
	 */
	@Column(name = "PWDERRCNT", nullable = false)
	private Integer pwdErrCnt;
	
	/**
	 * 上次密码错误时间 
	 */
	@Column(name = "LASTPWDERRTIME", nullable = true)
	private Date lastPwdErrTime;
	
	/**
	 * 开卡时间
	 */
	@Column(name = "OPENTIME", nullable = false)
	private Date openTime;
	
	/**
	 * 开卡状态
	 */
	@Column(name = "OPENSTATUS", nullable = false)
	private String openStatus;
	
	/**
	 * 销卡状态
	 */
	@Column(name = "CLOSESTATUS", nullable = false)
	private String closeStatus;
	
	/**
	 *  上次挂失/解挂时间
	 */
	@Column(name = "LASTLOSSTIME", nullable = true)
	private Date lastLossTime;
	
	/**
	 * 挂失状态
	 */
	@Column(name = "LOSSSTATUS", nullable = false)
	private String lossStatus;
	
	/**
	 * 手工锁定状态
	 */
	@Column(name = "MANUALLOCKSTATUS", nullable = false)
	private String manualLockStatus;
	
	/**
	 * 激活MAC
	 */
	@Column(name = "ACTIVATEMAC", nullable = true)
	private String activateMac; 
	
	/**
	 * 累计消费次数
	 */
	@Column(name = "TOTALCONSUMECOUNT", nullable = false)
	private BigInteger totalConsumeCount; 
	
	/**
	 * 原始卡号
	 */
	@Column(name = "ORIGCARDNO", nullable = true)
	private String origCardNo; 
	
	/**
	 * 换卡后卡号
	 */
	@Column(name = "NEWCARDNO", nullable = true)
	private String newCardNo; 

	public String getBatchNo() {
		return batchNo;
	}

	public void setBatchNo(String batchNo) {
		this.batchNo = batchNo;
	}

	public String getCardSeqNo() {
		return cardSeqNo;
	}

	public void setCardSeqNo(String cardSeqNo) {
		this.cardSeqNo = cardSeqNo;
	}

	public String getCardNo() {
		return cardNo;
	}

	public void setCardNo(String cardNo) {
		this.cardNo = cardNo;
	}

	public BigInteger getCardFaceId() {
		return cardFaceId;
	}

	public void setCardFaceId(BigInteger cardFaceId) {
		this.cardFaceId = cardFaceId;
	}

	public BigInteger getCardClassicId() {
		return cardClassicId;
	}

	public void setCardClassicId(BigInteger cardClassicId) {
		this.cardClassicId = cardClassicId;
	}

	public BigInteger getMasterAccNo() {
		return masterAccNo;
	}

	public void setMasterAccNo(BigInteger masterAccNo) {
		this.masterAccNo = masterAccNo;
	}

	public BigInteger getWalletAccNo() {
		return walletAccNo;
	}

	public void setWalletAccNo(BigInteger walletAccNo) {
		this.walletAccNo = walletAccNo;
	}
	
	public String getCardPwd() {
		return cardPwd;
	}

	public void setCardPwd(String cardPwd) {
		this.cardPwd = cardPwd;
	}

	public String getTwoTrackContent() {
		return twoTrackContent;
	}

	public void setTwoTrackContent(String twoTrackContent) {
		this.twoTrackContent = twoTrackContent;
	}

	public String getThirdTrackContent() {
		return thirdTrackContent;
	}

	public void setThirdTrackContent(String thirdTrackContent) {
		this.thirdTrackContent = thirdTrackContent;
	}

	public String getSaleBranchNo() {
		return saleBranchNo;
	}

	public void setSaleBranchNo(String saleBranchNo) {
		this.saleBranchNo = saleBranchNo;
	}

	public String getOpenBranchNo() {
		return openBranchNo;
	}

	public void setOpenBranchNo(String openBranchNo) {
		this.openBranchNo = openBranchNo;
	}

	public BigInteger getSaleOrderId() {
		return saleOrderId;
	}

	public void setSaleOrderId(BigInteger saleOrderId) {
		this.saleOrderId = saleOrderId;
	}

	public BigInteger getBuyerId() {
		return buyerId;
	}

	public void setBuyerId(BigInteger buyerId) {
		this.buyerId = buyerId;
	}

	public BigInteger getOwnerId() {
		return ownerId;
	}

	public void setOwnerId(BigInteger ownerId) {
		this.ownerId = ownerId;
	}

	public BigInteger getInitialBalance() {
		return initialBalance;
	}

	public void setInitialBalance(BigInteger initialBalance) {
		this.initialBalance = initialBalance;
	}

	public BigInteger getTotalDeposit() {
		return totalDeposit;
	}

	public void setTotalDeposit(BigInteger totalDeposit) {
		this.totalDeposit = totalDeposit;
	}

	public BigInteger getBalance() {
		return balance;
	}

	public void setBalance(BigInteger balance) {
		this.balance = balance;
	}

	public BigInteger getWalletBalance() {
		return walletBalance;
	}

	public void setWalletBalance(BigInteger walletBalance) {
		this.walletBalance = walletBalance;
	}
	
	public Date getExpired() {
		return expired;
	}

	public void setExpired(Date expired) {
		this.expired = expired;
	}

	public Date getLastExpiredFeeDate() {
		return lastExpiredFeeDate;
	}

	public void setLastExpiredFeeDate(Date lastExpiredFeeDate) {
		this.lastExpiredFeeDate = lastExpiredFeeDate;
	}

	public Integer getPwdErrCnt() {
		return pwdErrCnt;
	}

	public void setPwdErrCnt(Integer pwdErrCnt) {
		this.pwdErrCnt = pwdErrCnt;
	}

	public Date getLastPwdErrTime() {
		return lastPwdErrTime;
	}

	public void setLastPwdErrTime(Date lastPwdErrTime) {
		this.lastPwdErrTime = lastPwdErrTime;
	}

	public Date getOpenTime() {
		return openTime;
	}

	public void setOpenTime(Date openTime) {
		this.openTime = openTime;
	}

	/**
	 * 正常
	 */
	public final static String ACTIVATECARD_OPENSTATUS_NORMAL = "0";

	/**
	 * 换发新卡
	 */
	public final static String ACTIVATECARD_OPENSTATUS_ISSUE_NEW = "1";

	public String getOpenStatus() {
		return openStatus;
	}

	public void setOpenStatus(String openStatus) {
		this.openStatus = openStatus;
	}

	/**
	 * 未销卡
	 */
	public final static String ACTIVATECARD_CLOSESTATUS_NORMAL = "0";

	/**
	 * 赎回销卡
	 */
	public final static String ACTIVATECARD_CLOSESTATUS_CLOSED = "1";

	/**
	 * 补发新卡销卡
	 */
	public final static String ACTIVATECARD_CLOSESTATUS_CLOSED_ISSUE_NEW = "2";

	public String getCloseStatus() {
		return closeStatus;
	}

	public void setCloseStatus(String closeStatus) {
		this.closeStatus = closeStatus;
	}

	public Date getLastLossTime() {
		return lastLossTime;
	}

	public void setLastLossTime(Date lastLossTime) {
		this.lastLossTime = lastLossTime;
	}

	/**
	 * 正常
	 */
	public final static String ACTIVATECARD_LOSSSTATUS_NORMAL = "0";

	/**
	 * 临时挂失
	 */
	public final static String ACTIVATECARD_LOSSSTATUS_TEMP_LOSSED = "1";

	/**
	 * 正式挂失
	 */
	public final static String ACTIVATECARD_LOSSSTATUS_LOSSED = "2";

	/**
	 * 解挂失
	 */
	public final static String ACTIVATECARD_LOSSSTATUS_LOSSED_FREE = "3";

	public String getLossStatus() {
		return lossStatus;
	}

	public void setLossStatus(String lossStatus) {
		this.lossStatus = lossStatus;
	}

	/**
	 * 未锁定
	 */
	public final static String ACTIVATECARD_MANUALLOCKSTATUS_UNLOCK = "0";

	/**
	 * 锁定
	 */
	public final static String ACTIVATECARD_MANUALLOCKSTATUS_LOCKED = "1";

	/**
	 * 解除锁定
	 */
	public final static String ACTIVATECARD_MANUALLOCKSTATUS_LOCKED_FREE = "2";

	public String getManualLockStatus() {
		return manualLockStatus;
	}

	public void setManualLockStatus(String manualLockStatus) {
		this.manualLockStatus = manualLockStatus;
	}

	public String getActivateMac() {
		return activateMac;
	}

	public void setActivateMac(String activateMac) {
		this.activateMac = activateMac;
	}

	public BigInteger getTotalConsumeCount() {
		return totalConsumeCount;
	}

	public void setTotalConsumeCount(BigInteger totalConsumeCount) {
		this.totalConsumeCount = totalConsumeCount;
	}

	public String getOrigCardNo() {
		return origCardNo;
	}

	public void setOrigCardNo(String origCardNo) {
		this.origCardNo = origCardNo;
	}

	public String getNewCardNo() {
		return newCardNo;
	}

	public void setNewCardNo(String newCardNo) {
		this.newCardNo = newCardNo;
	}

	/**
	 * 检查密码的合法性
	 * 
	 * @param newPwd
	 *            明文密码
	 * @return boolean true - 成功 false - 失败
	 */
	public static boolean CheckPassword(String newPwd) {
		if (PCPSUtil.isEmpty(newPwd))
			return (false);
		if (newPwd.length() < 6 || newPwd.length() > 8)
			return (false);
		char ch;
		for (int i = 0; i < newPwd.length(); i++) {
			ch = newPwd.charAt(i);
			if (ch < '0' || ch > '9')
				return (false);
		}
		return (true);
	}

	/**
	 * 根据卡号和明文密码计算密文密码
	 * 
	 * @param CardNo
	 *            卡号
	 * @param Pwd
	 *            明文密码
	 * @return String 密文密码
	 */
	public static String CalcPassword(String CardNo, String Pwd) {
		String data = CardNo + Pwd;

		return new com.turing.post.util.SHA1().getDigestOfString(data
				.getBytes());
	}

	/**
	 * 验证卡密码
	 * 
	 * @param Pwd
	 *            明文密码
	 * @return boolean true - 成功 false - 失败
	 */
	public boolean VerifyPassword(String Pwd) {
		String encpwd = CalcPassword(getCardNo(), Pwd);
		return (encpwd.equalsIgnoreCase(getCardPwd()));
	}

	/**
	 * 修改卡密码
	 * 
	 * @param Pwd
	 *            明文密码
	 * @return boolean true - 成功 false - 失败
	 */
	public boolean ChangePassword(String Pwd) {
		// 计算密文密码
		String encpwd = CalcPassword(getCardNo(), Pwd);

		// 设置密文密码
		setCardPwd(encpwd);

		return (true);
	}
	
	public static void main(String[] args) throws Exception {
		System.out.println(
		BigDecimal.valueOf(10).multiply(
				BigDecimal.valueOf(499950)).divide(BigDecimal.valueOf(10000),0,RoundingMode.HALF_UP).toBigInteger());
		System.out.println(ActivateCard.CalcPassword("1300091248568567", "11111111")); 
		System.out.println(BigDecimal.valueOf(99.50).divide(BigDecimal.valueOf(1.0), 0, RoundingMode.HALF_UP).toBigInteger());
	}
}
